//
//  WXApiManager.m
//  SDKSample
//
//  Created by Jeason on 16/07/2015.
//
//

#import "WXApiManager.h"

@implementation WXApiManager

#pragma mark - LifeCycle
+(instancetype)sharedManager {
    static dispatch_once_t onceToken;
    static WXApiManager *instance;
    dispatch_once(&onceToken, ^{
        instance = [[WXApiManager alloc] init];
    });
    return instance;
}

#pragma mark - WXApiDelegate
- (void)onResp:(BaseResp *)resp {
//    if([resp isKindOfClass:[PayResp class]]){
//        //支付返回结果，实际支付结果需要去微信服务器端查询
//        NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"];
//
//        switch (resp.errCode) {
//            case WXSuccess:
//                strMsg = @"支付结果：成功！";
//                NSLog(@"支付成功－PaySuccess，retcode = %d", resp.errCode);
//                break;
//
//            default:
//                strMsg = [NSString stringWithFormat:@"支付结果：失败！retcode = %d, retstr = %@", resp.errCode,resp.errStr];
//                NSLog(@"错误，retcode = %d, retstr = %@", resp.errCode,resp.errStr);
//                break;
//        }
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//        [alert show];
//    }else {
//    }
    
    if ([resp isKindOfClass:[PayResp class]]) {
        
        PayResp *response = (PayResp *)resp;
//        if (_delegate && [_delegate respondsToSelector:@selector(managerDidRecvPaymentResponse:)]) {
//            [_delegate managerDidRecvPaymentResponse:response];
//        }
            if (self.delegate && [self.delegate respondsToSelector:@selector(managerDidRecvPaymentResponse:)])
            {
                [self.delegate managerDidRecvPaymentResponse:response];
            }
    }
    

}

- (void)onReq:(BaseReq *)req {

}


- (void)weiXinPayWithDic:(NSDictionary *)data {
    
//    PayReq *req = [[PayReq alloc] init];
//    req.openID = [wechatPayDic objectForKey:@"appId"];
//    req.partnerId = [wechatPayDic objectForKey:@"partnerId"];
//    req.prepayId = [wechatPayDic objectForKey:@"prepayId"];
//    req.package = [wechatPayDic objectForKey:@"packages"];
//    req.nonceStr = [wechatPayDic objectForKey:@"nonceStr"];
//    req.timeStamp = [[wechatPayDic objectForKey:@"timesTamp"] intValue];
//    req.sign = [wechatPayDic objectForKey:@"sign"];
//    [WXApi sendReq:req];
//    time_t now;
//    time(&now);
//    NSString *timestamp = [NSString stringWithFormat:@"%ld",now];
    
    //需要创建这个支付对象
    PayReq *req   = [[PayReq alloc] init];
    //由用户微信号和AppID组成的唯一标识，用于校验微信用户
    req.openID = data[@"appid"];
    // 商家id，在注册的时候给的
    req.partnerId = data[@"partnerid"];
    // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给你们服务器的，你们服务器再传给你
    req.prepayId  = data[@"prepayid"];
    // 根据财付通文档填写的数据和签名
                req.package = data[@"package"];
    // 随机编码，为了防止重复的，在后台生成
    req.nonceStr  = data[@"noncestr"];
    // 这个是时间戳，也是在后台生成的，为了验证支付的
    //            NSString * stamp = timestamp;
    req.timeStamp = [data[@"timestamp"] intValue];
    // 这个签名也是后台做的
    req.sign = data[@"sign"];
    //发送请求到微信，等待微信返回onResp
    [WXApi sendReq:req];
    
}

@end
